package mil.af.amc.adpetracker.web.actions.HandReceipt;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import mil.af.amc.adpetracker.model.HandReceipt;
import mil.af.amc.adpetracker.model.TemplateFile;
import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.service.HandReceiptService;
import mil.af.amc.adpetracker.service.TemplateFileService;
import mil.af.amc.adpetracker.web.actions.AbstractAction;
import org.apache.commons.io.IOUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;


/**
 * Please fill in this Javadoc
 *
 * @author TSgt Ferguson
 */
public class Create1297Action extends AbstractAction {

    private TemplateFileService templateFileService;

    public void setTemplateFileService(TemplateFileService templateFileService) {
        this.templateFileService = templateFileService;
    }

    private HandReceiptService handReceiptService;

    public void setHandReceiptService(HandReceiptService handReceiptService) {
        this.handReceiptService = handReceiptService;
    }

    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        int hrId = Integer.parseInt(httpServletRequest.getParameter("hrId"));
        HandReceipt hr = handReceiptService.getById(hrId);

        if (hr == null) {
            ActionMessage message = new ActionMessage("messages.handreceipt.notfound", "Unable to retrieve handreceipt id#" + hrId);
            saveError(httpServletRequest, message);

            return actionMapping.findForward("success");
        }

        // Get the InputStream of the 1297 PDF and convert it to a BAOS
        TemplateFile form1297 = templateFileService.getByName("AF1297.PDF");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(form1297.getTemplateFile(), baos);

        PdfReader reader = new PdfReader(form1297.getTemplateFile());
        PdfStamper stamper = new PdfStamper(reader, baos);
        PdfContentByte over = stamper.getOverContent(1);

        // Put the items on the hand receipt
        int length = 0;
        int lineHeight = 30;
        int startPoint = 600;
        for (EquipmentItem item : hr.getIdentifiedItems()) {

            if (length < 11) {
                over.beginText();
                over.setTextMatrix(startPoint, 100);
                over.showText(item.getSerial() + "                  " + item.getDescription());
                over.endText();

                length++;
                startPoint += lineHeight;
            }
        }

        stamper.close();

        Document doc = new Document();
        PdfWriter.getInstance(doc, baos);

        httpServletResponse.setContentType("application/pdf");
        httpServletResponse.setHeader("Content-Disposition", " inline; filename=handreceipt.pdf");

        ServletOutputStream os = httpServletResponse.getOutputStream();
        baos.writeTo(os);
        os.flush();

        return null;
    }
}
